package com.zxy.study.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxy.study.R;

public class TextTabLayout extends LinearLayout implements View.OnClickListener {

    //-----自定义属性
    private CharSequence[] tabTexts;
    private int normalColor;
    private int selectColor;
    private int normalTextSize;

    private int divideColor;
    private int divideWidth;
    private int divideHeight;
    /**View的距离*/
    private int dividePadding;
    private boolean showDividerLine;
    //----

    private TextView[] mTabViews;
    private OnTextTabListener mOnTextTabListener;


    public TextTabLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        init(context, attrs);
    }

    public TextTabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TextTabLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextTabLayout);

        tabTexts = ta.getTextArray(R.styleable.TextTabLayout_text_array);
        normalColor = ta.getColor(R.styleable.TextTabLayout_normal_text_color, Color.BLACK);
        selectColor = ta.getColor(R.styleable.TextTabLayout_select_text_color, Color.RED);
        normalTextSize = ta.getDimensionPixelSize(R.styleable.TextTabLayout_normal_text_size, 48);

        divideColor = ta.getColor(R.styleable.TextTabLayout_divider_line_color, Color.GRAY);
        divideWidth = ta.getDimensionPixelOffset(R.styleable.TextTabLayout_divider_line_width, 3);
        divideHeight = ta.getDimensionPixelOffset(R.styleable.TextTabLayout_divider_line_height, 30);
        dividePadding = ta.getDimensionPixelOffset(R.styleable.TextTabLayout_divide_padding, 24);
        showDividerLine = ta.getBoolean(R.styleable.TextTabLayout_show_divider_line, true);

        ta.recycle();

        addTextTab(tabTexts);
    }

    /**
     * 添加标签
     *
     * @param tabArray
     */
    public void addTextTab(CharSequence[] tabArray) {
        if (tabArray == null) return;
        this.tabTexts = tabArray;
        mTabViews = new TextView[tabTexts.length];

        if (getChildCount() > 0) {
            removeAllViews();
        }

        boolean isLastTab;
        for (int i = 0; i < tabTexts.length; i++) {
            mTabViews[i] = createTabView(tabTexts[i]);
            mTabViews[i].setId(i);

            isLastTab = (i + 1) == tabTexts.length;

            LinearLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.rightMargin = dividePadding;
            if (!isLastTab) {
                params.gravity = Gravity.CENTER_VERTICAL;
            }

            addView(mTabViews[i], params);
            mTabViews[i].setOnClickListener(this);

            //判断是否需要分割线
            if (showDividerLine && !isLastTab) {
                LinearLayout.LayoutParams dividerParams = new LayoutParams(divideWidth, divideHeight);
                dividerParams.rightMargin = dividePadding;
                dividerParams.gravity = Gravity.CENTER_VERTICAL;

                addView(createDividerView(), dividerParams);
            }
        }


    }

    private TextView createTabView(CharSequence tabText) {
        TextView textView = new TextView(getContext());
        textView.setText(tabText);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,normalTextSize);
        textView.setTextColor(normalColor);
        return textView;
    }

    private View createDividerView() {
        View view = new View(getContext());
        view.setBackgroundColor(divideColor);
        return view;
    }

    public void setOnTextTabListener(OnTextTabListener mOnTextTabListener) {
        this.mOnTextTabListener = mOnTextTabListener;
    }

    @Override
    public void onClick(View view) {
        int clickIndex = -1;
        for (int i = 0; i < mTabViews.length; i++) {
            if(mTabViews[i] == view){
                clickIndex = i;
                mTabViews[i].setTextColor(selectColor);
            }else {
                mTabViews[i].setTextColor(normalColor);
            }
        }

        if(mOnTextTabListener != null && clickIndex != -1){
            mOnTextTabListener.onTabClick(clickIndex);
        }
    }

    public interface OnTextTabListener{
        void onTabClick(int pos);
    }
}